'''
Created on Jun 21, 2012

@author: thomas_dao
'''
from web import base
from web.base import BaseHandler
from web import urls
from web import util
import json
import logging
from google.appengine.ext import ndb
from google.appengine.ext.ndb import model
from webapp2_extras.appengine.auth.models import User
from unique import unique
     
class CreateHandler(BaseHandler):
    @base.user_required
    def post(self):
        pass    

class ReadHandler(BaseHandler):
    def get(self):
        user = util.get_current_user_key().get()
        self.response.write(json.dumps(util.ndb_to_dict(user)))
    

class UpdateHandler(BaseHandler):
    def post(self):
        pass
    

class DeleteHandler(BaseHandler):
    def post(self):
        pass

class EditEmailHandler(BaseHandler):
    def post(self):
        emails = json.loads(self.request.params.get('emails'))
        user = util.get_current_user_key().get()
        try:
            emails = emails + user.emails + user.auth_ids
        except:
            pass
        user.emails = unique(emails)
        user.put()
        self.response.write(json.dumps(util.ndb_to_dict(user)))
    
class EditTimezoneHandler(BaseHandler):
    def post(self):
        timezone = self.request.params.get('timezone')
        user = util.get_current_user_key().get()
        user.timezone = timezone
        user.put()
        self.response.write(json.dumps(util.ndb_to_dict(user)))
    
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()